//
//  ADProgress.swift
//  ADKit
//
//  Created by Adoma on 2020/10/26.
//

import UIKit
import Then
import SnapKit

public class ADProgress: UIView {
    
    private let progressView = UIView()
    private var widthConstraint: Constraint!
    
    public var progress: Double = 0 {
        didSet {
            self.widthConstraint.update(offset: CGFloat(progress) * self.frame.width)
            if progress == 1 {
                DispatchQueue.main.after(time: 0.25) {
                    self.progressView.isHidden = true
                }
            } else {
                progressView.isHidden = false
            }
        }
    }
    
    public var color = ADKit.Color.k0170C9 {
        didSet {
            progressView.backgroundColor = color
        }
    }

    public init() {
        super.init(frame: .zero)
        
        progressView.backgroundColor = color
        addSubview(progressView)
        progressView.snp.makeConstraints { (make) in
            make.left.top.bottom.equalToSuperview()
            self.widthConstraint = make.width.equalTo(0).constraint
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        ADLogger(adkit: Self.self)
            .debug(#function)
    }
    
}
